﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sanford.Multimedia.Midi;

namespace WpfHero
{
    public static class MessageExtensions
    {
        public static bool IsTextType(this MetaMessage msg)
        {
            return (msg.MetaType == MetaType.Copyright ||
                msg.MetaType == MetaType.CuePoint ||
                msg.MetaType == MetaType.DeviceName ||
                msg.MetaType == MetaType.InstrumentName ||
                msg.MetaType == MetaType.Lyric ||
                msg.MetaType == MetaType.Marker ||
                msg.MetaType == MetaType.ProgramName ||
                msg.MetaType == MetaType.Text ||
                msg.MetaType == MetaType.TrackName);
        }

        public static int GetDifficulty(this ChannelMessage msg)
        {
            int pitch = msg.Data1;

            if (pitch >= 60 && pitch <= 64)
                return 1;

            if (pitch >= 72 && pitch <= 76)
                return 2;

            if (pitch >= 84 && pitch <=88)
                return 3;

            if (pitch >= 96 && pitch <= 100)
                return 4;

            return 0;
        }

        /// <remarks>1 - lowest button, 5 = highest. 0 if cannot be assigned to a button.</remarks>
        public static int GetButtonNumber(this ChannelMessage msg)
        {
            int basePitch = msg.GetDifficulty() * 12 + 48;
            int result = msg.Data1 - basePitch;
            if (result < 5)
                return result + 1;

            return 0;
        }
    }
}
